package com.sinping.qh.api.app.version.v1.messageInfo;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.domain.user.MessageInfoDomain;
import com.sinping.qh.domain.user.UserInfo;
import com.sinping.qh.service.MongoManager;
import com.sinping.qh.service.app.MessageInfo.IMessageInfoService;
import com.sinping.qh.service.app.auth.ISessionMongoService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(value = "app通知消息",description = "app通知消息API")
@RequestMapping(value = Const.APP_URI_V1 +"/appmessageinfoendpoint",name = "app通知消息")
@RestController
public class AppMessageInfoEndPoint extends BaseAppEndPoint {

    @Autowired
    IMessageInfoService iMessageInfoService;

    @Autowired
    MongoManager mongoManager;

    @Autowired
    ISessionMongoService sessionMongoService;

    @ApiOperation(value = "获取最新通知")
    @RequestMapping(value = "/getlatestdata",method = RequestMethod.GET)
    public ResDto getLatestMessageInfoEndPoint(HttpServletRequest request){
        ResDto resDto = iMessageInfoService.getLatestMessageInfo(request);
        return resDto;

    }

    @ApiOperation(value = "获取通知消息分页列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResDto getMessageInfoFrontPage(HttpServletRequest httpServletRequest,@ApiParam(name = "msg",value = "{\"page_size\":10, \"page_current\":1,\"condition\":{\"title\":\"123\",\"type\":\"0\",\"start_time\":\"2018-06-05 00:00:00\",\"end_time\":\"2018-06-08 00:00:00\"}}") @RequestParam(required = true) String msg){
        FrontPage<MessageInfoDomain> frontPage = JsonMapper.defaultMapper().fromJson(msg,FrontPage.class);
        checkNull(frontPage,ConstEum.PARAM_ERROR);
        String token =httpServletRequest.getHeader(Const.TOKEN_NAME);
        com.sinping.qh.dto.mongo.auth.UserSessionDTO sessionByToken = getUserSessionDto();

        UserInfo userInfo = sessionByToken.getUserInfo();

        String userId = userInfo.getId();
        ResDto resDto = iMessageInfoService.getMessageInfoFrontPage(userId,frontPage);
        return resDto;
    }
}
